<script setup>
import { ref, onUnmounted, nextTick } from "vue";
import { send } from "@/services/login";

import { useUserStore } from "@/stores/user.js";

import { appLogout } from "@/services/login";
import { version } from "@/services/index";
import { onShow } from "@dcloudio/uni-app";
const SystemVersion = ref("");
const Version = ref({});

const value = ref();
const showSelect = ref("");
// const version = ref("");
if (uni.getStorageSync("closeBroadcast")) {
  value.value = false;
} else {
  value.value = true;
}

if (uni.getStorageSync("selectShopType")) {
  showSelect.value = uni.getStorageSync("selectShopType");
}

const isPass = ref(
  uni.getStorageSync("isPass") ? uni.getStorageSync("isPass") : true
);
onShow(() => {
  // #ifdef APP-PLUS
  if (uni.getSystemInfoSync) {
    let systemInfo = uni.getSystemInfoSync();

    SystemVersion.value = systemInfo.appWgtVersion;
  } else if (plus) {
    // 非H5平台，例如App或小程序
    // console.log('应用版本: ' + plus.runtime.version); // 应用版本
    SystemVersion.value = plus.runtime.version;
  }

  // #endif
});

const change = (e) => {
  console.log(e);
  if (e) {
    uni.removeStorageSync("closeBroadcast");
  } else {
    uni.setStorageSync("closeBroadcast", true);
  }
  console.log("dsadas", uni.getStorageSync("closeBroadcast"));
};

const sendfun = async () => {
  let res = await appLogout();
  if (res.code == 200) {
    uni.removeStorageSync("X-Auth-Token");

    uni.removeStorageSync("more");
    uni.removeStorageSync("user");
    uni.navigateTo({ url: "/pages/login/login" });
  }
};
const go = (url) => {
  uni.navigateTo({ url: url });
};

const getVersion = async () => {
  // #ifdef APP-PLUS

  let res = await version();

  Version.value = res.data;
  let isUpdate = compare(Version.value.version, SystemVersion.value);
  if (isUpdate == 1) {
    upDate();
  } else if (isUpdate == 0) {
    uni.showToast({
      title: "当前是最新版本",
      icon: "none",
    });
  } else if (isUpdate == -1) {
    console.log("更新过小");
    uni.showToast({
      title: "暂无需要更新版本",
      icon: "none",
    });
  }
  // #endif
};

function compare(v1 = "0", v2 = "0") {
  v1 = String(v1).split(".");
  v2 = String(v2).split(".");
  const minVersionLens = Math.min(v1.length, v2.length);

  let result = 0;
  for (let i = 0; i < minVersionLens; i++) {
    const curV1 = Number(v1[i]);
    const curV2 = Number(v2[i]);

    if (curV1 > curV2) {
      result = 1;
      break;
    } else if (curV1 < curV2) {
      result = -1;
      break;
    }
  }
  return result;
}

const upDate = () => {
  //console.log('进入方法',data.data.appPath)
  uni.downloadFile({
    url: Version.value.url,
    success: function (downloadResult) {
      console.log("downloadResult", downloadResult);
      if (downloadResult.statusCode === 200) {
        uni.showModal({
          title: "有新版本更新",
          content: "是否下载更新",
          success: function (res) {
            if (res.confirm) {
              //console.log('用户点击确定');

              plus.runtime.install(
                downloadResult.tempFilePath,
                {
                  force: true,
                },
                function () {
                  uni.showToast({
                    title: "安装成功",
                    icon: 2000,
                    duration: 1000,
                  });
                  setTimeout(function () {
                    plus.runtime.restart();
                  }, 1100);
                },
                function (e) {
                  // })
                  console.log(e);
                  uni.showToast({
                    title: "安装失败",
                    icon: 2000,
                    duration: 1000,
                  });
                }
              );
            } else if (res.cancel) {
              console.log("用户点击取消");
              return;
            }
          },
        });
      }
    },
    fail: function (res) {
      console.log("失败");
    },
    error: function (res) {
      console.log(res);
    },
  });
};
</script>
<template>
  <view class="bg">
    <top-nav :title="'设置'" :border="false" rType="save"></top-nav>
    <view class="setting">
      <view class="setting-item" v-if="showSelect == 'shop'">
        <view>
          <text>收银播报提示音</text>
          <view class="dec"
            >{{
              value
                ? "已开启，收款将自动播报语音"
                : "已关闭，收款将不会播报语音"
            }}
          </view>
        </view>

        <up-switch
          inactiveColor="#E5E6EB"
          v-model="value"
          @change="change"
        ></up-switch>
      </view>
      <view
        class="setting-item"
        @click="go('/pages/user_about/index?from=sys_user_agree')"
      >
        <view>
          <text>服务协议</text>
        </view>

        <view
          ><up-icon name="arrow-right" color="#2979ff" size="28rpx"></up-icon
        ></view>
      </view>
      <view
        class="setting-item"
        @click="go('/pages/user_about/index?from=sys_user_privacy')"
      >
        <view>
          <text>隐私政策</text>
        </view>

        <view
          ><up-icon name="arrow-right" color="#2979ff" size="28rpx"></up-icon
        ></view>
      </view>
      <view
        v-if="isPass"
        class="setting-item"
        @click="go('/pages/mysetting/upphone/index')"
      >
        <view>
          <text>修改密码</text>
        </view>

        <view
          ><up-icon name="arrow-right" color="#2979ff" size="28rpx"></up-icon
        ></view>
      </view>

      <!-- #ifdef APP-PLUS -->
      <view class="setting-item">
        <view>
          <text>版本信息</text>
        </view>

        <view>{{ SystemVersion }}</view>
      </view>

      <view class="setting-item" @click="getVersion">
        <view>
          <text>版本更新</text>
        </view>
        <view
          ><up-icon name="arrow-right" color="#2979ff" size="28rpx"></up-icon
        ></view>
      </view>
      <!-- #endif -->
    </view>

    <view class="setting-btn">
      <up-button color="#1679FB" @click="sendfun">退出登录</up-button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
